package com.wzy.orderSystem.dao;

import com.wzy.orderSystem.util.ConfigManage;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.*;

public class FoodDao {
    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;


    static  final String PATH = "D:\\web_project\\src\\main\\resources\\database1.properties";
    static  final  String DRIVER = ConfigManage.getConfigManage(PATH).getValue("driver");
    static  final  String URL = ConfigManage.getConfigManage(PATH).getValue("url");
    static  final  String USER = ConfigManage.getConfigManage(PATH).getValue("user");
    static  final  String PASS = ConfigManage.getConfigManage(PATH).getValue("password");

    public static Connection getConnection(){
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USER, PASS);
//            System.out.println(connection);
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
        return connection;
    }
//    public static Connection getConnection(){
//        try {
//            Context context = new InitialContext();
//            String jndi = "java:comp/env/jdbc/foods";
//            DataSource source = (DataSource) context.lookup(jndi);
//            connection = source.getConnection();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return connection;
//    }

    public static void closeConnection(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(preparedStatement != null){
                preparedStatement.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    //根据参数查询
    public static ResultSet selectByArgs(String sql, Object[] obj){
//        ResultSet resultSet = null;
        try {
             connection = getConnection();
             preparedStatement = connection.prepareStatement(sql);
             if(obj != null){
                 for (int i=0; i<obj.length; ++i){
                     preparedStatement.setObject(i+1,obj[i]);
                 }
                 resultSet = preparedStatement.executeQuery();
             }else{
                 resultSet =preparedStatement.executeQuery();
             }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            FoodDao.closeConnection(resultSet,preparedStatement,connection);
        }
        return resultSet;
    }

    //根据参数 增 删 改
    public static int updateByArgs(String sql, Object[] obj){
        int rows =0;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if(obj != null){
                for(int i = 1; i<=obj.length; ++i){
                    preparedStatement.setObject(i,obj[i-1]);
                }
                rows = preparedStatement.executeUpdate();
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            closeConnection(resultSet,preparedStatement,connection);
        }
        return rows;
    }
}
